#include <iostream>
#include <string>


#include "grpc_db.h"


void RunServer() {
	std::string configFilePath = "./config/db_grpc_config.json";
	Json::Value root;
	Json::Reader reader;
	std::ifstream configFile(configFilePath, std::ifstream::binary);
	bool parsingSuccessful = reader.parse(configFile, root);
	if (!parsingSuccessful) {
		std::cerr << "Failed to parse configuration file(grpc)\n\a" << std::endl;
		exit(-1);
	}
	std::string ip = root["ip"].asString();
	std::string port = root["port"].asString();



	configFile.close();
	std::string server_address(ip + ":" + port);
	DatabaseServiceImpl service;
	service.db.printDriverInfo();
	service.db.printConnectInfo();


	grpc::ServerBuilder builder;
	builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
	builder.RegisterService(&service);

	std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
	std::cout << "Server listening on " << server_address << std::endl;
	server->Wait();
}


int main() {

	RunServer();

	return 0;
}



/*
	db.printDriverInfo();
	db.printConnectInfo();

	db.userInsert(2110551202, "甘劲博", "031207Gjb_");
	db.userInsert(2110551201, "王海涛", "147258369");

	stack_chat_db::user_info uf;
	uf.id = 2110551202,
		uf.age = 20,
		uf.email = "123@qq.com",
		uf.gtime = "2023",
		uf.info = "你好世界",
		uf.phone = "12345789",
		uf.sex = "男";
	db.user_infoInsert(uf);
	uf.id = 2110551201;
	db.user_infoInsert(uf);

	stack_chat_db::message_log ml;
	ml.id = 1,
		ml.data = "你好世界",
		ml.receive_id = 2110551202,
		ml.send_id = 2110551201,
		ml.time = "2024",
		ml.status = 2;
	db.message_logInsert(ml);
	ml.id = 2,
		ml.data = "世界你好";
	db.message_logInsert(ml);

	//std::cout << db.message_logGetMessageByUserID(2110551202) << std::endl;
	std::cout << db.friend_tableGetFriendsByUserID(2110551202) << std::endl;

	stack_chat_db::friend_table ft;
	ft.friend_id = 3, ft.user_id_a = 2110551201, ft.user_id_b = 2110551201, ft.friend_time = "12345679";

	std::cout << db.friend_tableIsFriend(2110551201, 2110551202) << std::endl;


	db.friend_tableInsert(ft);
	std::cout << db.friend_tableGetFriendsByUserID(2110551202) << std::endl;
	std::cout << db.friend_tableGetFriendsByUserID(2110551201) << std::endl;


	db.friend_tableDeleteByUserID(2110551201, 2110551201);
	std::cout << db.friend_tableGetFriendsByUserID(2110551202) << std::endl;
	std::cout << db.friend_tableGetFriendsByUserID(2110551201) << std::endl;


*/